package org.example.member.service.impl;

import org.example.core.constant.TokenConstants;
import org.example.member.dao.MemberReceiveAddressDao;
import org.example.member.entity.MemberReceiveAddressEntity;
import org.example.security.handler.SecurityHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.example.member.service.MemberReceiveAddressService;

import java.util.List;


@Service("memberReceiveAddressService")
public class MemberReceiveAddressServiceImpl implements MemberReceiveAddressService {

    @Autowired
    private MemberReceiveAddressDao memberReceiveAddressDao;

    // 获取会员的收货地址
    @Override
    public List<MemberReceiveAddressEntity> queryAddressByMemberId() {
        String memberId = SecurityHandler.getData().get(TokenConstants.MEMBER_ID);
        return memberReceiveAddressDao.getAddressByMemberId(Long.parseLong(memberId));
    }

    /**
     * 根据地址id查询地址信息
     * @param addressId
     * @return
     */
    @Override
    public MemberReceiveAddressEntity queryAddressInfoById(Long addressId) {
        return memberReceiveAddressDao.getAddressInfoById(addressId);
    }

    // 修改地址信息
    @Override
    public int updateMemberAddress(Long id, MemberReceiveAddressEntity memberReceiveAddress) {
        long memberId = Long.parseLong(SecurityHandler.getData().get(TokenConstants.MEMBER_ID));
        if (memberReceiveAddress.getDefaultStatus() == 0){
            memberReceiveAddressDao.updateAddressStatus(memberId);
        }
        return memberReceiveAddressDao.updateMemberAddress(id, memberReceiveAddress, memberId);
    }

    // 新增地址
    @Override
    public int addMemberAddress(MemberReceiveAddressEntity memberReceiveAddress) {
        long memberId = Long.parseLong(SecurityHandler.getData().get(TokenConstants.MEMBER_ID));
        if (memberReceiveAddress.getDefaultStatus() == 0){
            memberReceiveAddressDao.updateAddressStatus(memberId);
        }
        return memberReceiveAddressDao.addMemberAddress(memberId, memberReceiveAddress);
    }

    // 删除地址
    @Override
    public int deleteAddressById(Long id) {
        long memberId = Long.parseLong(SecurityHandler.getData().get(TokenConstants.MEMBER_ID));
        return memberReceiveAddressDao.deleteMemberAddress(id, memberId);
    }
}